﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using VupSlashLauncher.Common;

namespace VupSlashLauncher.View
{
    public partial class IMessageBox : Window
    {
        /// <summary>
        /// 弹出消息框，显示提示信息
        /// </summary>
        /// <param name="title">弹窗标题</param>
        /// <param name="msg">提示信息</param>
        /// <param name="yestext">确认按钮文本</param>
        /// <param name="notext">取消按钮文本</param>
        /// <param name="showicon">是否显示图标</param>
        /// <returns></returns>
        public static bool Show(string msg = null, string title = null, string yestext = null, string notext = null, bool showicon = false)
        {
            IMessageBox msgBox = new IMessageBox() { Title = title, Message = msg, YesLabel = yestext, NoLabel = notext, ShowIcon = showicon };
            bool? state = msgBox.ShowDialog();
            if (state == true) return true;
            else return false;
        }

        private IMessageBox()
        {
            InitializeComponent();
        }

        private string yesText = "Yes";
        private string noText = "No";
        private bool showIcon = false;
        public new string Title
        {
            get { return this.TitleLabel.Text; }
            set { this.TitleLabel.Text = value; }
        }
        public string Message
        {
            get { return this.MsgLabel.Text; }
            set { this.MsgLabel.Text = value; }
        }
        public string YesLabel
        {
            get { return this.yesText; }
            set { this.yesText = value; }
        }
        public string NoLabel
        {
            get { return this.noText; }
            set { this.noText = value; }
        }
        public bool ShowIcon
        {
            get { return this.showIcon; }
            set { this.showIcon = value; }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Resources.Add("TColor", new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalValues.ThemeColor)));
            Color c = (Color)ColorConverter.ConvertFromString((GlobalValues.ThemeColor));
            if ((c.R * 30 + c.G * 59 + c.B * 11 + 50) / 100 > 160) { this.Resources.Add("CColor", new SolidColorBrush(Color.FromRgb(0, 0, 0))); }
            else { this.Resources.Add("CColor", new SolidColorBrush(Color.FromRgb(255, 255, 255))); }

            if (yesText == null) YesButton.Visibility = Visibility.Collapsed;
            else YesButton.Content = "  " + yesText + "  ";
            if (noText == null) NoButton.Visibility = Visibility.Collapsed;
            else NoButton.Content = "  " + noText + "  ";

            if (showIcon)
            {
                ErrorImage.Visibility = Visibility.Visible;
                MsgLabel.Margin = new Thickness(90, 0, 0, 0);
            }
        }

        private void WindowMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.DragMove();
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            var story = (Storyboard)this.Resources["WindowDisappearAnimation"];
            if (story != null)
            {
                story.Completed += delegate { Close(); };
                story.Begin(this);
            }
        }
        private void Path_Loaded(object sender, RoutedEventArgs e)
        {
            RotateTransform transform = new RotateTransform();
            ErrorImage.RenderTransform = transform;
            ErrorImage.RenderTransformOrigin = new Point(0.5, 0.5);
            Storyboard storyboard = new Storyboard();
            DoubleAnimation Rotateanimation1 = new DoubleAnimation(0, -30, TimeSpan.FromSeconds(.2)) { AutoReverse = true, BeginTime = TimeSpan.FromSeconds(1) };
            storyboard.Children.Add(Rotateanimation1);
            Storyboard.SetTarget(Rotateanimation1, ErrorImage);
            Storyboard.SetTargetProperty(Rotateanimation1, new PropertyPath("RenderTransform.Angle"));
            DoubleAnimation Rotateanimation2 = new DoubleAnimation(0, 30, TimeSpan.FromSeconds(.2)) { AutoReverse = true, BeginTime = TimeSpan.FromSeconds(1.4) };
            storyboard.Children.Add(Rotateanimation2);
            Storyboard.SetTarget(Rotateanimation2, ErrorImage);
            Storyboard.SetTargetProperty(Rotateanimation2, new PropertyPath("RenderTransform.Angle"));
            storyboard.Begin();
        }
        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            (sender as Button).IsEnabled = false;
            var story = (Storyboard)this.Resources["WindowDisappearAnimation"];
            if (story != null)
            {
                story.Completed += delegate { this.DialogResult = true; };
                story.Begin(this);
            }
        }
        private void NoButton_Click(object sender, RoutedEventArgs e)
        {
            (sender as Button).IsEnabled = false;
            var story = (Storyboard)this.Resources["WindowDisappearAnimation"];
            if (story != null)
            {
                story.Completed += delegate { this.DialogResult = false; };
                story.Begin(this);
            }
        }

        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.Space)
            {
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4)
            {
                e.Handled = true;
                CloseButton_Click(null, null);
            }
            else if (e.Key == Key.Escape) CloseButton_Click(null, null);
        }

    }
}
